package com.etl.controller;

import com.etl.entity.EtlTaskIncrement;
import com.etl.enums.ResultEnum;
import com.etl.model.rev.EtlTaskListRev;
import com.etl.service.IEtlTaskIncrementService;
import com.etl.util.ResponseUtil;
import com.etl.util.ResultVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2019/10/21.
 */
@RestController
@RequestMapping("/etlIncrementTask")
@Api(tags = {"增量任务管理"}, value = "任务配置相关接口")
public class EtlTaskIncrementController {

    @Autowired
    private IEtlTaskIncrementService etlIncrementTaskTaskService;

    @ApiOperation(value = "获取某业务系统下的所有任务")
    @PutMapping(value = "/list")
    public ResultVO<PageInfo<EtlTaskIncrement>> list(@RequestBody EtlTaskListRev etlTaskListRev) {
        if (etlTaskListRev.getPages() == null || etlTaskListRev.getSizes() == null) {
            return ResponseUtil.error(ResultEnum.PARAMS_ERROR);
        }
        PageInfo<EtlTaskIncrement> pageInfo = etlIncrementTaskTaskService.getEtlIncrementTaskList(etlTaskListRev);
        return ResponseUtil.success(pageInfo);
    }

    @ApiOperation(value = "添加ETL任务")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "table", value = "表名", example = "cm_project", required = true),
            @ApiImplicitParam(paramType = "form", name = "sql1", value = "sql1", example = "查询语句", required = true),
            @ApiImplicitParam(paramType = "form", name = "batch", value = "系统名称", example = "1", required = true),
            @ApiImplicitParam(paramType = "form", name = "comment", value = "表的描述", example = "项目表", required = true),
            @ApiImplicitParam(paramType = "form", name = "tableType", value = "表类型(大小写)", example = "1", required = true)
    })
    @PutMapping(value = "/add")
    public ResultVO addTask(@RequestBody EtlTaskIncrement etlTaskIncrement) {
//        if (Strings.isBlank(table) || Strings.isBlank(sql1) || Strings.isBlank(comment) || batch == null || tableType == null) {
//            return ResponseUtil.error(ResultEnum.PARAMS_ERROR);
//        }
        etlIncrementTaskTaskService.addEtlIncrementTask(etlTaskIncrement);
        return ResponseUtil.success();
    }


    @ApiOperation(value = "修改ETL任务")
    @PutMapping(value = "/updateEtlIncrementTask")
    public ResultVO updateEtlIncrementTask(@RequestBody EtlTaskIncrement etlTaskIncrement) {
        etlIncrementTaskTaskService.updateEtlIncrementTask(etlTaskIncrement);

        return ResponseUtil.success();
    }

    @ApiOperation(value = "执行单个ETL任务")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "kid", value = "任务主键", example = "13", required = true)
    })
    @GetMapping(value = "/executeIncrementSingle")
    public ResultVO executeSingle(Long kid) {
        etlIncrementTaskTaskService.executeIncrementSingle(kid,"增量抽取作业","增量抽取作业");
        return ResponseUtil.success();
    }


    @ApiOperation(value = "执行批次下所有任务")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "batch", value = "批次号", example = "1", required = true)
    })
    @GetMapping(value = "/executeAll")
    public ResultVO executeAll(Integer batch) {
        etlIncrementTaskTaskService.delayExecuteAll(batch);
        return ResponseUtil.success();
    }




    @ApiOperation(value = "删除任务与表结构")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "kid", value = "任务主键", example = "13", required = true),
            @ApiImplicitParam(paramType = "query", name = "type", value = "删除类型，type=1时 只删除任务，", example = "13", required = true),
    })
    @GetMapping(value = "/removeEtlIncrementTaskAndTable")
    public ResultVO removeTaskAndTable(Long kid, Integer type) {
        etlIncrementTaskTaskService.removeTaskAndTable(kid, type);
        return ResponseUtil.success();
    }
}

